/**
 * 
 */
package com.thinkgsp.jxc.service.impl;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgsp.jxc.model.RpAPayBill;
import com.thinkgsp.jxc.model.RpARecBill;
import com.thinkgsp.jxc.model.RpArpBill;
import com.thinkgsp.jxc.model.StockInBill;
import com.thinkgsp.jxc.model.StockOutBill;
import com.thinkgsp.jxc.persistence.mybatis.PagingResult;
import com.thinkgsp.jxc.service.RpArpBillService;
import com.thinkgsp.jxc.service.ServiceException;

/**
 * @author evan
 *
 */
@Service("arpBillService")
@Transactional
public class RpArpBillServiceImpl extends RpServiceImpl implements
		RpArpBillService {

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpArpBillService#getArpBill(java.lang.Long)
	 */
	public <T extends RpArpBill> T getArpBill(Long id) {
		return arpBillMapper.getArpBill(id);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpArpBillService#getARecBillList(java.sql.Date, java.sql.Date)
	 */
	public List<RpARecBill> getARecBillList(Date begDate, Date endDate) {
		return arpBillMapper.getArpBillList(begDate, endDate, 1);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpArpBillService#getAPayBillList(java.sql.Date, java.sql.Date)
	 */
	public List<RpAPayBill> getAPayBillList(Date begDate, Date endDate) {
		return arpBillMapper.getArpBillList(begDate, endDate, -1);
	}


	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpArpBillService#createAPayBillFromStockInBill(java.lang.Long)
	 */
	public Long createAPayBillFromStockInBill(Long stkBillId) throws ServiceException {
		StockInBill bill = (StockInBill)billMapper.getBill(stkBillId);
		if(!bill.getArchived()) {
			throw new ServiceException("入库单未归档，不允许生成应付账款");
		}
		BigDecimal billAmount = bill.getBillAmount().getBillSettleAmount();
		if(billAmount.compareTo(BigDecimal.ZERO) == 0) return 0L;
		RpAPayBill apayBill = new RpAPayBill();
		apayBill.setArpBillCode(bill.getBillCode());
		apayBill.setCustSuppId(bill.getCustId());
		apayBill.setDeptId(bill.getDeptId());
		apayBill.setEmpId(bill.getEmpId());
		apayBill.setMakerId(bill.getMakerId());
		apayBill.setAmount(billAmount);
		apayBill.setJxcBillId(bill.getBillId());
		apayBill.setFyear(bill.getFyear());
		apayBill.setPeriod(bill.getPeriod());
		apayBill.setRemark(bill.getRemark());
		arpBillMapper.insertArpBill(apayBill);
		
		archiveAPayBill(apayBill.getArpBillId());
		
		return apayBill.getArpBillId();
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpArpBillService#createARecBillFromStockOutBill(java.lang.Long)
	 */
	public Long createARecBillFromStockOutBill(Long stkBillId) throws ServiceException {
		StockOutBill bill = (StockOutBill)billMapper.getBill(stkBillId);
		if(!bill.getArchived()) {
			throw new ServiceException("出库单未归档，不允许生成应付账款");
		}		
		BigDecimal billAmount = bill.getBillAmount().getBillSettleAmount();
		if(billAmount.compareTo(BigDecimal.ZERO) == 0) return 0L;
		RpARecBill arecBill = new RpARecBill();
		arecBill.setArpBillCode(bill.getBillCode());
		arecBill.setCustSuppId(bill.getCustId());
		arecBill.setDeptId(bill.getDeptId());
		arecBill.setEmpId(bill.getEmpId());
		arecBill.setMakerId(bill.getMakerId());
		arecBill.setAmount(billAmount);
		arecBill.setJxcBillId(bill.getBillId());
		arecBill.setFyear(bill.getFyear());
		arecBill.setPeriod(bill.getPeriod());
		arecBill.setRemark(bill.getRemark());
		arpBillMapper.insertArpBill(arecBill);
		
		archiveARecBill(arecBill.getArpBillId());
		
		return arecBill.getArpBillId();
	}

	public <T extends RpArpBill> T getArpBillFromStockBill(Long stockBillId) {
		return arpBillMapper.getArpBillFromStockBill(stockBillId);
	}

	public List<RpARecBill> getARecBillList(Date begDate, Date endDate,
			Integer custId) {
		return arpBillMapper.getArpBillListWithCust(begDate, endDate, custId, 1);
	}

	public List<RpAPayBill> getAPayBillList(Date begDate, Date endDate,
			Integer custId) {
		return arpBillMapper.getArpBillListWithCust(begDate, endDate, custId, -1);
	}

	public PagingResult<RpARecBill> getARecBillList(Date begDate, Date endDate,
			int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", 1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		RowBounds rows = new RowBounds(start,size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListCount", 
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListPaging", 
				params, rows);
	}

	public PagingResult<RpARecBill> getARecBillList(Date begDate, Date endDate,
			Integer custId, int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", 1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		params.put("custId", custId);
		RowBounds rows = new RowBounds(start,size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListWithCustCount", 
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListWithCustPaging", 
				params, rows);
	}

	public PagingResult<RpAPayBill> getAPayBillList(Date begDate, Date endDate,
			int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", -1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		RowBounds rows = new RowBounds(start,size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListCount", 
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListPaging", 
				params, rows);
	}

	public PagingResult<RpAPayBill> getAPayBillList(Date begDate, Date endDate,
			Integer custId, int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", -1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		params.put("custId", custId);
		RowBounds rows = new RowBounds(start,size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListWithCustCount", 
				"com.thinkgsp.jxc.persistence.RpArpBillMapper.getArpBillListWithCustPaging", 
				params, rows);
	}	
}
